// pages/personCenter/editAddress/editAddress.js
var telReg = /^(0|86|17951)?(13[0-9]|14[0-9]|15[012356789]|16[0-9]|17[0-9]|18[0-9]|19[0-9]|)[0-9]{8}$/;
var utils = require("../../../utils/util.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    tel:'',
    detail:'',
    is_default:1,
    area:'',
    disabled:true,
    customItem:'市辖区'
  },
  //获取地址
  getArea:function(e){
    console.log(e.detail);
    this.setData({
      area: e.detail.value[0] + ',' + e.detail.value[1] + ',' + e.detail.value[2]
    });
  },
  getTel:function(e){
    this.setData({
      tel: e.detail.value
    });
  },
  //获取姓名
  getName:function(e){
    this.setData({
      name: e.detail.value
    });
  },
  getDetail:function(e){
    console.log(e.detail);
    this.setData({
      detail: e.detail.value
    });
  },
  switchChange:function(e){
   let num=e.detail.value?2:1;
   console.log(num);
    this.setData({
      is_default: num
    });
  },
  submit:function(){
    let me=this;
    if(this.data.name==''){
      wx.showToast({
        title:'请输入收货人姓名',
        icon:'none'
      });
    } else if (this.data.tel==''){
      wx.showToast({
        title: '请输入收货人手机号',
        icon: 'none'
      });
    } else if (!telReg.test(this.data.tel)) {
      wx.showToast({
        title: '收货人手机号格式不正确',
        icon: 'none'
      });
    } else if (this.data.area==""){
      wx.showToast({
        title: '请选择省、市、区',
        icon: 'none'
      });
    } else if (this.data.detail==''){
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      });
    } else {
      let domi = '/api/v1/myself/addReceivingAddress',requestType="POST",dataPackage = {
        name: me.data.name,
        phone: me.data.tel,
        province_name: me.data.area.split(',')[0],
        city_name: me.data.area.split(',')[1],
        area_name: me.data.area.split(',')[2],
        address: me.data.detail,
        is_default: me.data.is_default,
        token: app.globalData.token
      };
      if(this.data.id){
        domi ='/api/v1/myself/editReceivingAddress';
        dataPackage.user_address_id = this.data.id;
        requestType="GET";
      };
      wx.request({
        url: utils.dominame +domi, //仅为示例，并非真实的接口地址
        data: dataPackage,
        method: requestType,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data);
          if (res.data.code==0){
            wx.showToast({
              title: res.data.msg,
              icon: 'success',
              mask:true,
              success:function(){
                setTimeout(()=>{
                  wx.navigateBack({
                    delta: 1
                  });
                },2000);
              }
            });
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            });
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.info) {
      let info = JSON.parse(decodeURI(options.info));
      console.log(info);
      this.setData({
        name: info.name,
        tel: info.phone,
        detail: info.address,
        is_default: info.is_default,
        id: info.id,
        area: info.province_name + ',' + info.city_name + ',' + info.area_name,
      });
    }
    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})